<?php
namespace MkUser\Controller;

use Org\Util\String;
use Think\Controller;

class LoginController extends Controller
{
    public function index()
    {
        if (getLoginFlag() === true)
        {
            redirect(APP_ROOT . DEF_MODULE);
        }
        else
        {
            $this->assign("empt_var", "");
            $this->assign("title_befor", "登录 | ");
            $this->assign("cur_year", strftime("%Y", time()));
            $this->assign("user_name", strval(cookie("user_name")));
            $this->assign("rem_login", strval(cookie("rem_login")));
            $this->display();
        }
    }

    public function login()
    {
        if (IS_POST == true)
        {
            // 记录界面参数
            // if (I("post.rem_login") == "true")
            // {
            //     cookie("rem_login", "checked", intval(COOKIE_EXPIRES_MAX));
            //     cookie("user_name", I("post.user_name"), intval(COOKIE_EXPIRES_MAX));
            // }
            // else
            // {
            //     cookie("rem_login", null);
            //     cookie("user_name", null);
            // }

            //--------------------------------------------------
            // 输入合法性判断
            if (trim(I("post.user_name")) == "")
            {
                $result_ary = array("success" => 0, "info" => "登录用户名不能为空");
                $this->ajaxReturn($result_ary);
            }

            // 登录操作
            $User                   = M("User");
            $condition["user_name"] = I("post.user_name");
            $result_data            = $User->where($condition)->select();
            if (count($result_data) === 0)
            {
                $result_ary = array("success" => 0, "info" => "登录用户不存在");
                $this->ajaxReturn($result_ary);
            }

            // 登录操作
            $User                   = M("User");
            $condition["user_name"] = I("post.user_name");
            $condition["password"]  = getPassWordStr(I("post.password"));
            $result_data            = $User->where($condition)->select();
            if (count($result_data) > 0)
            {
                // 登录成功
                // $info_list = array(
                //     "user.dis_name"       => $result_data[0]["dis_name"],
                //     "user.manage_auth"    => $result_data[0]["manage_auth"],
                //     "user.gc_manage_auth" => $result_data[0]["gc_manage_auth"]
                // );
                $info_list["user.dis_name"] = $result_data[0]["dis_name"];
                foreach (M("User")->getDbFields() as $k => $v)
                {
                    if (strpos($v, "_manage_auth") !== false)
                        $info_list["user." . $v] = $result_data[0][$v];
                }
                setSessionList($info_list);
                setLoginFlag();

                // 返回数据
                $result_ary = array("success" => 1, "callback" => APP_ROOT . DEF_MODULE);
                $this->ajaxReturn($result_ary);
            }
            else
            {
                // 登录失败
                $result_ary = array("success" => 0, "info" => "登录密码错误");
                $this->ajaxReturn($result_ary);
            }
        }
    }

    public function logout()
    {
        setLogout();
    }
}